<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Default.php 713 2011-05-11 02:38:25Z ma.rongliang $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Adapter for authentication
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Default.php 713 2011-05-11 02:38:25Z ma.rongliang $$
 */
class Evebit_Auth_Adapter_Default implements Zend_Auth_Adapter_Interface {
	
	/**
	 * 
	 * @var bool
	 */
	protected $isGoogleLogin = true;
	
	/**
	 * 
	 * @var The filed for getting the user data
	 */
	protected $identityField = 'email';
	/**
	 * @var stirng
	 */
	protected $identity = null;
	
	/**
	 * @var stirng
	 */
	protected $password = null;
	
	/**
	 * @var string
	 */
    protected $toragetype = null;
    /**
	 * @var string
	 */
    protected $checkCode = null;
      /**
	 * @var string
	 */
    protected $token = null;
     /**
	 * @var string
	 */
    protected $captchaUrl = null;
      /**
	 * @var string
	 */
    protected $captchaToken = null;
	
	/**
	 * @var string
	 */
	protected $modelTypeClass = 'Varien_Object';
	
	/**
	 * @var array
	 */
	protected $options = array('identify_field'=> 'name', 'password_field'=> 'password');
	
	/**
	 * Sets identity and password for authentication
	 *
	 * @return void
	 */
	public function __construct($options) {
		if ($options) {
			$this->options = array_merge($this->options, $options);
		}
	}
	
	/**
	 * Get Option
	 * @param string $field
	 * @return string
	 */
	public function getOption($field = null) {
		if ($field === null) {
			return $this->options;
		} else if (isset($this->options[$field])) {
			return $this->options[$field];
		} else {
			return null;
		}
	}
	
	public function getIsGoogleLogin(){
		return $this->isGoogleLogin;
	}
	
	public function setIsGoogleLogin($isGoogleLogin){
		$this->isGoogleLogin = $isGoogleLogin;
	}
	
	public function getIdentityField(){
		return $this->identityField;
	}
	
	public function setIdentityField($filed){
		$this->identityField = $filed;
	}
	/**
	 * @return the $identity
	 */
	public function getIdentity() {
		return $this->identity;
	}
	
	/**
	 * @param $identity the $identity to set
	 */
	public function setIdentity($identity) {
		$this->identity = $identity;
	}
	
	/**
	 * @return the $password
	 */
	public function getPassword() {
		return $this->password;
	}
	
	/**
	 * @param $password the $password to set
	 */
	public function setPassword($password) {
		$this->password = $password;
	}
    public function getToragetype() {
		return $this->toragetype;
	}
	public function setToragetype($toragetype) {
		$this->toragetype = $toragetype;
	}
    public function getCheckCode() {
		return $this->checkCode;
	}
	public function setCheckCode($checkCode) {
		$this->checkCode = $checkCode;
	}
    public function getToken() {
		return $this->token;
	}
	public function setToken($token) {
		$this->token = $token;
	}	
	public function getCaptchaUrl() {
		return $this->captchaUrl;
	}
	public function setCaptchaUrl($captchaUrl) {
		$this->captchaUrl=$captchaUrl;
	}
	public function getCaptchaToken() {
		return $this->captchaToken;
	}
	public function setCaptchaToken($captchaToken) {
		$this->captchaToken=$captchaToken;
	}	
	/**
	 * @return the $modelTypeClass
	 */
	public function getModelTypeClass() {
		return $this->modelTypeClass;
	}
	
	/**
	 * @param $modelTypeClass the $modelTypeClass to set
	 */
	public function setModelTypeClass($modelTypeClass) {
		$this->modelTypeClass = $modelTypeClass;
	}
	
	/**
	 * Seach model by identity
	 * 
	 * @return Varien_Object
	 */
	protected function fetchModel() {
		$model = null;
		if ($this->getIdentity()) {
			$type = $this->getModelTypeClass();
			$model = new $type();
			$collection = $model->getCollection();
			$collection->addFieldToFilter($this->options['identify_field'], $this->getIdentity());
			$model = $collection->getFirstItem();
		}
		return $model;
	}
	
	/**
	 * Performs an authentication attempt
	 *
	 * @throws Zend_Auth_Adapter_Exception If authentication cannot
	 * be performed
	 * @return Zend_Auth_Result
	 */
	public function authenticate() {
		$code = Zend_Auth_Result::FAILURE_UNCATEGORIZED;
		$identity = null;
		$messages = array();
		
		$session = Evebit_Application::getSession();
		
		$model = $this->fetchModel();
		
		if (!$model || !$model->getId()) {
			$code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
			$messages[] = Evebit_Application::__("Account not found");
		} else {
			$validHash = $model->getData($this->options['password_field']);
			$hash = $model->getHash($this->getPassword(), $validHash);
			if ($hash == $validHash) {
				$code = Zend_Auth_Result::SUCCESS;
				$identity = $model->getId();
				$messages[] = Evebit_Application::__("Login succuess");
			} else {
				$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
				$messages[] = Evebit_Application::__("Password invalid");
			}
		}
		
		return new Zend_Auth_Result($code, $identity, $messages);
	}
}
